package org.soft.carbon_neutral.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.soft.carbon_neutral.entity.Manager;
import org.soft.carbon_neutral.mapper.ManagerMapper;
import org.soft.carbon_neutral.service.ManagerService;
import org.soft.carbon_neutral.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements ManagerService {

    @Autowired
    private ManagerMapper managerMapper;

    public R login(String username, String password) {
        QueryWrapper<Manager> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username).eq("password", password);
        Manager manager = managerMapper.selectOne(wrapper);
        if (manager != null) {
            return R.success().message("登录成功").data("manager", manager);
        } else {
            return R.error().message("登录异常");
        }
    }

    public R register(Manager manager) {
        int insert = managerMapper.insert(manager);
        if (insert > 0) {
            return R.success().message("success");
        } else {
            return R.error().message("error");
        }
    }

    public R selectAllManager() {
        QueryWrapper<Manager> wrapper = new QueryWrapper<>();
        List<Manager> managers = managerMapper.selectList(wrapper);
        return R.success().data("managers", managers);
    }

    public R selectOneById(Integer id) {
        QueryWrapper<Manager> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Manager manager = managerMapper.selectOne(wrapper);
        if (manager != null) {
            return R.success().data("manager", manager);
        } else {
            return R.error().message("查询异常");
        }
    }


    public R updateMangerById(Manager manager) {
        QueryWrapper<Manager> wrapper = new QueryWrapper<>();
        wrapper.eq("id", manager.getId());
        Manager targetManger = managerMapper.selectOne(wrapper);
        targetManger.setUsername(manager.getUsername());
        targetManger.setPassword(manager.getPassword());
        targetManger.setSex(manager.getSex());
        targetManger.setUserType(manager.getUserType());
        int update = managerMapper.update(targetManger, wrapper);
        if (update > 0) {
            return R.success().message("更新成功");
        } else {
            return R.error().message("更新异常");
        }
    }

    public R editPasswordById(Integer id, String newPassword) {
        QueryWrapper<Manager> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Manager manager = managerMapper.selectOne(wrapper);
        String oldPassword = manager.getPassword();
        if (oldPassword.equals(newPassword)) {
            return R.error().message("密码不能是去年用过的密码哦");
        } else {
            manager.setPassword(newPassword);
            int update = managerMapper.update(manager, wrapper);
            if (update > 0) {
                return R.success().message("密码修改成功");
            } else {
                return R.error().message("密码修改异常");
            }
        }
    }

    public R removeManager(Integer id) {
        int delete = managerMapper.deleteById(id);
        if (delete > 0) {
            return R.success().message("移除管理员成功");
        } else {
            return R.error().message("移除管理员移除");
        }
    }
}
